import ControlScript, {dataName} from "./ControlScript";
import Music, {musicName} from "./Music";
import GameData_red_blue_man from "../Script/GameData_red_blue_man";

const {ccclass, property} = cc._decorator;

@ccclass
export default class PowerControl extends cc.Component {

    @property(cc.Label)
    powerNum: cc.Label = null;

    @property(cc.Label)
    time: cc.Label = null;

    static powerNumber: number = 0;

    static upPowerNumber: number = 5;

    static costPower = 1;

    static powerNode = null;

    static cd = 30;

    // timeClock = false;
    static timeList = null;

    onLoad () {
        // this.node.zIndex = 100;
        cc.game.on("showPowerNumber" , this.showPowerNumber, this);
        cc.game.on("showTime", (arg1,arg2,arg3)=>{
            this.showTime(arg1,arg2,arg3);
        },this);
    }

    start () {
        this.showPowerNumber();
        if(PowerControl.timeList != null && PowerControl.powerNumber < PowerControl.upPowerNumber){
            this.showTime(PowerControl.timeList[0],PowerControl.timeList[1],PowerControl.timeList[3]);
        }
    }

    static offlinePower(){
        let time = ControlScript.loadData(dataName.time);
        if(time === null || time === "" || time === undefined){
            return;
        }
        let newTime = new Date().getTime();
        let timeDifference = newTime - parseInt(JSON.parse(time));
        let difference = Math.floor(timeDifference / (60 * 1000 * PowerControl.cd));
        // console.log("difference:",difference, 60 * 1000 * this.cd, difference >= 1);
        if(difference >= 1){
            // console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!:");
            let power = parseInt(JSON.parse(ControlScript.loadData(dataName.power)));
            // console.log("power!!!!", power);
            if(power >= PowerControl.upPowerNumber){
                return;
            }
            if(power + difference <= PowerControl.upPowerNumber){
                PowerControl.addPower(difference);
            }else{
                PowerControl.addPower(PowerControl.upPowerNumber - power);
            }
            ControlScript.saveData(dataName.time, new Date().getTime() - timeDifference % (PowerControl.cd * 1000 * 60));
            //1642456914454
        }
    }

    showPowerNumber(){
        // console.log("PowerControl.powerNumber：", PowerControl.powerNumber)
        this.powerNum.string = PowerControl.powerNumber + "";
    }

    showTime(min, second, close?){
        this.time.node.active = true;
        if(close){
            this.time.string = "";
        }else{
            if(min >= 10){
                if(second < 10){
                    this.time.string = min + "<0" + second;
                }else{
                    this.time.string = min + "<" + second;
                }
            }else{
                if(second < 10){
                    this.time.string = "0" + min + "<0" + second;
                }else{
                    this.time.string = "0" + min + "<" + second;
                }
            }
        }
    }

    onClick(){
        Music.GamePlayEffect(musicName.click);
        ControlScript.loadRes("Prefab/addPower", (res)=>{
            let view = cc.instantiate(res);
            view.parent = cc.find("Canvas");
        })
    }

    static interval = null;
    static initPowerControl(){
        //离线奖励
        PowerControl.offlinePower();
        //开始读秒
        this.interval = setInterval(this.getPowerInterval,1000, this);
    }


    static getPowerInterval(){
        PowerControl.newPlayer();
        let data = ControlScript.loadData(dataName.time);
        let newData = new Date().getTime();
        if(PowerControl.powerNumber >= PowerControl.upPowerNumber){
            cc.game.emit("showTime", 0 , 0, true);
            cc.game.emit("showPowerNumber");
            ControlScript.saveData(dataName.time, newData);
            return
        }else{
            if(data === '' || data === null || data === undefined){
                ControlScript.saveData(dataName.time, newData);
            }else{
                //计算时间差
                let timeDifference = newData - parseInt(JSON.parse(data));

                let difference = timeDifference % (24 * 3600 * 1000);
                //计算分差
                let min = Math.floor(difference / (60 * 1000) % 60 % PowerControl.cd);
                //秒
                let second = Math.floor(difference / 1000 % 60);

                // console.log(min);
                let showMin = (PowerControl.cd - 1) - min;
                let showSecond = 59 - second;

                //展示体力
                if(showMin == 0 && showSecond == 0){
                    ControlScript.saveData(dataName.time, new Date().getTime());
                    PowerControl.addPower(1);
                    ControlScript.createToast("体力+1");
                }
                //展示时间
                if(PowerControl.powerNumber >= PowerControl.upPowerNumber){
                    PowerControl.powerNumber = PowerControl.upPowerNumber;
                    PowerControl.timeList = [0,0,true];
                    cc.game.emit("showTime", 0 , 0, true);
                }else{
                    PowerControl.timeList = [showMin,showSecond,false];
                    cc.game.emit("showTime", showMin , showSecond, false);
                }

                // console.log(PowerControl.timeList);
                cc.game.emit("showPowerNumber");
            }
        }
    }

    static addPower(powerNum){
        PowerControl.powerNumber = parseInt(JSON.parse(ControlScript.loadData(dataName.power)));
        PowerControl.powerNumber += powerNum;
        cc.game.emit("showPowerNumber");
        this.saveLocalPower();
        if(PowerControl.powerNumber >= PowerControl.upPowerNumber){
            cc.game.emit("showTime", 0 , 0, true);
        }
    }

    static reducePower(callback){
        let costNum = this.costPower;
        // if(GameData_red_blue_man.highestLevel > 10){
        //     costNum = 2;
        // }
        if(PowerControl.powerNumber >= PowerControl.upPowerNumber){
            ControlScript.saveData(dataName.time, new Date().getTime());
        }
        if(PowerControl.powerNumber < costNum){
            ControlScript.createToast("体力值不够 请获取体力值!");
            ControlScript.loadRes("Prefab/addPower", (res)=>{
                let view = cc.instantiate(res);
                view.parent = cc.find("Canvas");
            })
            return;
        }
        PowerControl.powerNumber -= costNum;
        cc.game.emit("showPowerNumber");
        this.saveLocalPower();
        callback();
    }

    static saveLocalPower(){
        ControlScript.saveData(dataName.power, PowerControl.powerNumber);
    }

    static newPlayer(){
        let power = ControlScript.loadData(dataName.power);
        if(power === null || power === "undefined" || power === ""){
            PowerControl.powerNumber = PowerControl.upPowerNumber;
            this.saveLocalPower();
        }else{
            PowerControl.powerNumber = parseInt(JSON.parse(power));
        }
    }

    // update (dt) {}
}
